package com.wangyk.jpaproject.controller;

import com.wangyk.jpaproject.repository.ProductInfoRepository;
import com.wangyk.jpaproject.service.CategoryService;
import com.wangyk.jpaproject.service.ProductInfoService;
import com.wangyk.rabbitmqcommon.VO.CategoryVO;
import com.wangyk.rabbitmqcommon.VO.ProductInfoVO;
import com.wangyk.rabbitmqcommon.VO.ResultVO;
import com.wangyk.rabbitmqcommon.model.jpaproject.Category;
import com.wangyk.rabbitmqcommon.model.jpaproject.ProductInfo;
import com.wangyk.rabbitmqcommon.utils.ResultVOUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 产品信息方法
 * */
@Controller
@RequestMapping("/product")
@Slf4j
public class ProductInfoController
{
    @Autowired
    private ProductInfoRepository productInfoRepository;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductInfoService productInfoService;

    /**
     * 接口:localhost:8090/jpa-project/jpa/product/find_all_by_product_id.do?openId=aa&productInfoId=1
     * 根据商品的ID查询商品的信息。
     * @RequestParam: 从接口中取值。
     * @RequestBody: 从接口中取值。
     * */
    @RequestMapping("/find_all_by_product_id.do")
    @ResponseBody
    public ResultVO findAllByProductId(@RequestParam Integer productInfoId)
    {
        ResultVO resultVO=new ResultVO();
        log.info("productInfoId:"+productInfoId.toString());
        List<ProductInfo> productInfoList = productInfoService.findAllByProductId(productInfoId);
        log.info("size:"+productInfoList.size());
        log.info("test!");
        resultVO.setData(productInfoList);
        return ResultVOUtils.success(resultVO);
    }

    /**
     * 接口:localhost:8090/jpa-project/jpa/product/find_all_by_product_id_no_annotation.do?openId=aa&productInfoId=1
     * 根据商品的ID查询商品的信息。
     * @RequestParam: 从接口中取值。
     * */
    @RequestMapping("/find_all_by_product_id_no_annotation.do")
    @ResponseBody
    public ResultVO findAllByProductIdNoAnnotation(Integer productInfoId)
    {
        ResultVO resultVO=new ResultVO();
        List<ProductInfo> productInfoList = productInfoService.findAllByProductId(productInfoId);
        resultVO.setData(productInfoList);
        return ResultVOUtils.success(resultVO);
    }

    /**
     * 接口:localhost:8090/jpa-project/jpa/product/find_all_by_product_id_requestbody.do?openId=aa&productInfoId=1
     * 根据商品的ID查询商品的信息。
     * @RequestParam: 从接口中取值。
     * */
    @RequestMapping("/find_all_by_product_id_requestbody.do")
    @ResponseBody
    public ResultVO findAllByProductIdRequestBody(Integer productInfoId)
    {
        ResultVO resultVO=new ResultVO();
        List<ProductInfo> productInfoList = productInfoService.findAllByProductId(productInfoId);
        resultVO.setData(productInfoList);
        return ResultVOUtils.success(resultVO);
    }

    /**
     * 查询产品种类下的商品信息。
     * */
    @RequestMapping("/list.do")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public ResultVO<List<CategoryVO>> list()
    {
        try
        {
            ResultVO<List<CategoryVO>> resultVO = new ResultVO<List<CategoryVO>>();
            //1.查出所有的商品类别和商品信息
            List<Category> categoryList = categoryService.findAll();
            List<ProductInfo> productInfoList = productInfoService.findAll();
            //2.构造返回值zhi
            List<CategoryVO> categoryVOList = new ArrayList<CategoryVO>();
            List<ProductInfoVO> productInfoVOList = null;
            CategoryVO categoryVO = null;
            ProductInfoVO productInfoVO = null;
            for (Category category : categoryList)
            {
                categoryVO = new CategoryVO();
                BeanUtils.copyProperties(categoryVO, category);
                productInfoVOList = new ArrayList<ProductInfoVO>();
                for (ProductInfo productInfo : productInfoList)
                {
                    if (category.getCategoryId().equals(productInfo.getCategoryId()))
                    {
                        productInfoVO = new ProductInfoVO();
                        BeanUtils.copyProperties(productInfoVO, productInfo);
                        productInfoVOList.add(productInfoVO);
                    }
                }
                categoryVO.setProductInfoVOList(productInfoVOList);
                categoryVOList.add(categoryVO);
            }
            resultVO.setData(categoryVOList);
            return ResultVOUtils.success(resultVO);
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        catch (InvocationTargetException e)
        {
            e.printStackTrace();
        }
        return ResultVOUtils.error(null);
    }

    /**
     * 添加一条商品详情记录，修改一条商品记录详情。
     * */
    @RequestMapping("/save_product_info.do")
    @ResponseBody
    public ResultVO saveProductInfo(@RequestBody ProductInfo productInfo)
    {
        ResultVO resultVO=new ResultVO();
        productInfo= productInfoService.saveProductInfo(productInfo);
        resultVO.setData(productInfo);
        return ResultVOUtils.success(resultVO);
    }

    /**
     * 添加一条商品详情记录，修改一条商品记录详情。
     * */
    @RequestMapping("/save_product_info_by_selective.do")
    @ResponseBody
    public ResultVO saveProductInfoBySelective(@RequestBody ProductInfo productInfo)
    {
        log.info("controller中的productInfo:"+productInfo.toString());
        ResultVO resultVO=new ResultVO();
        productInfo= productInfoService.saveProductInfoBySelective(productInfo);
        resultVO.setData(productInfo);
        return ResultVOUtils.success(resultVO);
    }
}
